package com.hzsxy.bighomework.controller;

import com.hzsxy.bighomework.api.NotificationTotalView;
import com.hzsxy.bighomework.entity.*;
import com.hzsxy.bighomework.repository.ClassInfoRepository;
import com.hzsxy.bighomework.repository.NotificationClassRepository;
import com.hzsxy.bighomework.repository.NotificationRepository;
import com.hzsxy.bighomework.repository.StudentClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/notification")
public class NotificationController {
    @Autowired
    private NotificationRepository notificationRepository;
    @Autowired
    private ClassInfoRepository classInfoRepository;
    @Autowired
    private NotificationClassRepository notificationClassRepository;
    @Autowired
    private StudentClassRepository studentClassRepository;

    @GetMapping("/total")
    public String getTotal(Model model,HttpSession httpSession){
        String type = (String) httpSession.getAttribute("type");
        if ("student".equals(type)){
            StudentDO studentDO = (StudentDO) httpSession.getAttribute("info");
            List<StudentClassDO> studentClassDOList = studentClassRepository.findByStudentId(studentDO.getStudentId());
            List<NotificationTotalView> viewList = new ArrayList<>();
            Map<Integer,Integer> existMap = new HashMap<>();
            studentClassDOList.forEach(it -> {
                List<NotificationClassDO> notificationClassDOList = notificationClassRepository.findByClassId(it.getClassId());
                List<NotificationTotalView> tempView = notificationClassDOList.stream().filter( n -> {
                    if (existMap.get(n.getNotificationId())!=null && existMap.get(n.getNotificationId())!=0){
                        return false;
                    }else {
                        existMap.put(n.getNotificationId(),1);
                        return true;
                    }
                }).map( n -> {
                    NotificationDO notificationDO = notificationRepository.findOne(n.getNotificationId());
                    NotificationTotalView view = new NotificationTotalView();
                    view.setId(notificationDO.getId());
                    view.setAuthor(notificationDO.getAuthor());
                    view.setCreateTime(notificationDO.getCreateTime().toString());
                    view.setInfo(notificationDO.getInfo());
                    view.setLevel(notificationDO.getLevel());
                    return view;
                }).collect(Collectors.toList());
                viewList.addAll(tempView);
            });
            model.addAttribute("views",viewList);
            return "student/notification-total";
        }else if("admin".equals(type)){
            List<NotificationDO> notificationDOList = (List<NotificationDO>) notificationRepository.findAll();
            List<NotificationTotalView> viewList = notificationDOList.stream().map(it -> {
                NotificationTotalView view = new NotificationTotalView();
                view.setId(it.getId());
                view.setAuthor(it.getAuthor());
                view.setCreateTime(it.getCreateTime().toString());
                view.setInfo(it.getInfo());
                view.setLevel(it.getLevel());
                return view;
            }).collect(Collectors.toList());
            model.addAttribute("views",viewList);
            return "teacher/notification-total";
        }else{
            Teacher teacher = (Teacher) httpSession.getAttribute("info");
            List<NotificationDO> notificationDOList = notificationRepository.findByAuthor(teacher.getName());
            List<NotificationTotalView> viewList = notificationDOList.stream().map(it -> {
                NotificationTotalView view = new NotificationTotalView();
                view.setId(it.getId());
                view.setAuthor(it.getAuthor());
                view.setCreateTime(it.getCreateTime().toString());
                view.setInfo(it.getInfo());
                view.setLevel(it.getLevel());
                return view;
            }).collect(Collectors.toList());
            model.addAttribute("views",viewList);
            return "teacher/notification-total";
        }
    }

    @RequestMapping("/teacher/remove")
    @ResponseBody
    public String remove(@RequestParam int notificationId){
        try{
            notificationRepository.delete(notificationId);
            List<NotificationClassDO> notificationClassDOList = notificationClassRepository.findByNotificationId(notificationId);
            notificationClassRepository.delete(notificationClassDOList);
            return "succeeded";
        }catch (Exception ex){
            ex.printStackTrace();
            return "failed";
        }
    }

    @RequestMapping("/teacher/addPage")
    public String addPage(Model model){
        List<ClassInfoDO> classInfoDOList = classInfoRepository.findAll();
        model.addAttribute("classes",classInfoDOList);
        return "teacher/notification-add";
    }

    @RequestMapping("/teacher/add")
    @ResponseBody
    public String add(HttpServletRequest request, HttpSession httpSession){
        try{
            Teacher teacher = (Teacher) httpSession.getAttribute("info");
            String info = request.getParameter("info");
            String level = request.getParameter("level");
            List<String> classIdList = Arrays.asList(request.getParameterValues("classIds[]"));
            NotificationDO notificationDO = new NotificationDO();
            notificationDO.setInfo(info);
            notificationDO.setLevel(level);
            notificationDO.setAuthor(teacher.getName());
            notificationDO.setCreateTime(new Date());
            NotificationDO notification = notificationRepository.save(notificationDO);
            classIdList.forEach(it -> {
                NotificationClassDO notificationClassDO = new NotificationClassDO();
                notificationClassDO.setClassId(it);
                notificationClassDO.setNotificationId(notification.getId());
                notificationClassRepository.save(notificationClassDO);
            });
            return "succeeded";
        }catch (Exception ex){
            ex.printStackTrace();
            return "failed";
        }
    }
}
